package com.cw.jeeyt.service.impl.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.common.enums.EnumDataJavaType;
import com.cw.jeeyt.common.enums.EnumYn;
import com.cw.jeeyt.service.bo.api.ApiDataEntryBo;
import com.cw.jeeyt.service.bo.api.ApiFacadeObjParamBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiFacadeObjParamDao;
import com.cw.jeeyt.dao.model.api.ApiFacadeObjParamDo;
import com.cw.jeeyt.service.intf.api.ApiDataEntryService;
import com.cw.jeeyt.service.intf.api.ApiFacadeObjParamService;
import com.cw.jeeyt.service.converter.api.ApiFacadeObjParamConverter;
import com.cw.lang.common.utils.IdGenerator;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 接口方法参数对象具体参数业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class ApiFacadeObjParamServiceImpl implements ApiFacadeObjParamService {

    private static final Logger logger = LoggerFactory.getLogger(ApiFacadeObjParamServiceImpl.class);
    @Autowired
    private ApiFacadeObjParamDao apiFacadeObjParamDao;//接口方法参数对象具体参数数据库操作接口
    @Autowired
    private ApiDataEntryService apiDataEntryService;

    /**
     * 查询所有接口方法参数对象具体参数
     */
    @Override
    public List<ApiFacadeObjParamBo> queryApiFacadeObjParamList() throws Exception {
        return ApiFacadeObjParamConverter.doList2BoList(apiFacadeObjParamDao.queryApiFacadeObjParamList());
    }

    /**
     * 分页查询接口方法参数对象具体参数
     *
     * @param apiFacadeObjParamBo
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiFacadeObjParamBo> queryByPage(ApiFacadeObjParamBo apiFacadeObjParamBo, Integer pageNo,
        Integer pageSize, Order order) throws Exception {
        PageInfo<ApiFacadeObjParamDo> pageInfo = apiFacadeObjParamDao.queryByPage(
            ApiFacadeObjParamConverter.bo2Do(apiFacadeObjParamBo), pageNo, pageSize, order);
        return new PageInfo<>(pageInfo, ApiFacadeObjParamConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻接口方法参数对象具体参数信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiFacadeObjParamBo getApiFacadeObjParamById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        return ApiFacadeObjParamConverter.do2Bo(apiFacadeObjParamDao.getById(id));
    }

    /**
     * 新增接口方法参数对象具体参数
     *
     * @param apiFacadeObjParamBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ApiFacadeObjParamBo apiFacadeObjParamBo) throws Exception {

        if (apiFacadeObjParamBo == null) {
            logger.error("接口方法参数对象具体参数为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjParamDo apiFacadeObjParam = ApiFacadeObjParamConverter.bo2Do(apiFacadeObjParamBo);
        apiFacadeObjParam.setCreateBy(userVo.getId());
        apiFacadeObjParam.setCreateDate(new Date());
        apiFacadeObjParam.setUpdateBy(userVo.getId());
        apiFacadeObjParam.setUpdateDate(new Date());
        apiFacadeObjParam.setId(IdGenerator.nextId());
        Integer result = apiFacadeObjParamDao.insert(apiFacadeObjParam);
        return result;
    }

    /**
     * 新增接口方法参数对象具体参数不为空的字段
     *
     * @param apiFacadeObjParamBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ApiFacadeObjParamBo apiFacadeObjParamBo) throws Exception {

        if (apiFacadeObjParamBo == null) {
            logger.error("接口方法参数对象具体参数为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjParamDo apiFacadeObjParam = ApiFacadeObjParamConverter.bo2Do(apiFacadeObjParamBo);
        apiFacadeObjParam.setCreateBy(userVo.getId());
        apiFacadeObjParam.setCreateDate(new Date());
        apiFacadeObjParam.setUpdateBy(userVo.getId());
        apiFacadeObjParam.setUpdateDate(new Date());
        apiFacadeObjParam.setId(IdGenerator.nextId());
        Integer result = apiFacadeObjParamDao.insertSelective(apiFacadeObjParam);
        return result;
    }

    /**
     * 更新接口方法参数对象具体参数
     *
     * @param apiFacadeObjParamBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiFacadeObjParamBo apiFacadeObjParamBo) throws Exception {

        if (apiFacadeObjParamBo == null) {
            logger.error("接口方法参数对象具体参数为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjParamDo apiFacadeObjParam = ApiFacadeObjParamConverter.bo2Do(apiFacadeObjParamBo);
        apiFacadeObjParam.setUpdateBy(userVo.getId());
        apiFacadeObjParam.setUpdateDate(new Date());

        apiFacadeObjParamDao.update(apiFacadeObjParam);
    }

    /**
     * 更新接口方法参数对象具体参数不为空的字段
     *
     * @param apiFacadeObjParamBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiFacadeObjParamBo apiFacadeObjParamBo) throws Exception {

        if (apiFacadeObjParamBo == null) {
            logger.error("接口方法参数对象具体参数为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjParamDo apiFacadeObjParam = ApiFacadeObjParamConverter.bo2Do(apiFacadeObjParamBo);
        apiFacadeObjParam.setUpdateBy(userVo.getId());
        apiFacadeObjParam.setUpdateDate(new Date());

        apiFacadeObjParamDao.updateSelective(apiFacadeObjParam);
    }

    /**
     * 删除接口方法参数对象具体参数
     *
     * @param apiFacadeObjParamBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiFacadeObjParamBo apiFacadeObjParamBo) throws Exception {

        if (apiFacadeObjParamBo == null) {
            logger.error("接口方法参数对象具体参数为空");
            return;
        }
        apiFacadeObjParamDao.delete(ApiFacadeObjParamConverter.bo2Do(apiFacadeObjParamBo));
    }

    /**
     * 导入接口方法对象参数
     */
    @Override
    public boolean importDataEntry(String dataEntryIds, Long objId) throws Exception {
        boolean success = false;
        if (StringUtils.isEmpty(dataEntryIds) || objId == null) {
            throw new Exception("导入接口方法对象参数的入参为空，请确认！");
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        String[] dataEntryIdList = dataEntryIds.split(",");
        List<ApiFacadeObjParamBo> apiFacadeObjParamList = new ArrayList<ApiFacadeObjParamBo>();
        if (dataEntryIdList != null && dataEntryIdList.length > 0) {
            Integer i = 1;
            for (String dataEntryId : dataEntryIdList) {
                ApiDataEntryBo apiDataEntry = apiDataEntryService.getApiDataEntryById(Long.parseLong(dataEntryId));
                ApiFacadeObjParamBo apiFacadeObjParam = new ApiFacadeObjParamBo();
                apiFacadeObjParam.setIsRequired(EnumYn.Y.getCode());
                apiFacadeObjParam.setIsList(EnumYn.N.getCode());
                apiFacadeObjParam.setParamJavaType(apiDataEntry.getColumnType());
                String columnLength = apiDataEntry.getColumnLength();
                if (!StringUtils.isEmpty(columnLength)) {
                    apiFacadeObjParam.setMaxLength(Integer.parseInt(columnLength));
                }
                apiFacadeObjParam.setParamCode(apiDataEntry.getColumnCode());
                apiFacadeObjParam.setParamDesc(apiDataEntry.getColumnName());
                apiFacadeObjParam.setObjId(objId);
                apiFacadeObjParam.setOrder(i);
                apiFacadeObjParam.setCreateBy(userVo.getId());
                apiFacadeObjParam.setCreateDate(new Date());
                apiFacadeObjParam.setUpdateBy(userVo.getId());
                apiFacadeObjParam.setUpdateDate(new Date());
                apiFacadeObjParam.setParamDemo("-");
                apiFacadeObjParamList.add(apiFacadeObjParam);
                i++;
            }
        }
        if (apiFacadeObjParamList != null && !apiFacadeObjParamList.isEmpty()) {
            insertBat(apiFacadeObjParamList);
            success = true;
        }
        return success;
    }

    /**
     * 批量新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertBat(List<ApiFacadeObjParamBo> apiFacadeObjParamList) throws Exception {

        if (apiFacadeObjParamList == null || apiFacadeObjParamList.isEmpty()) {
            logger.error("接口方法参数对象具体参数为空");
            return false;
        }
        Integer result = apiFacadeObjParamDao.insertBat(
            ApiFacadeObjParamConverter.boList2DoList(apiFacadeObjParamList));
        return result > 0;

    }

    /**
     * 根据对象id查询对象参数集合信息
     */
    @Override
    public List<ApiFacadeObjParamBo> queryListByObjId(Long objId) {
        if (objId == null) {
            logger.error("对象id不能为空");
            return null;
        }
        List<ApiFacadeObjParamBo> facadeObjParamBos = ApiFacadeObjParamConverter.doList2BoList(
            apiFacadeObjParamDao.queryListByObjId(objId));
        if (facadeObjParamBos != null && !facadeObjParamBos.isEmpty()) {
            for (ApiFacadeObjParamBo facadeObjParamBo : facadeObjParamBos) {
                String paramJavaType = facadeObjParamBo.getParamJavaType();
                String isRequired = facadeObjParamBo.getIsRequired();
                if (!StringUtils.isEmpty(paramJavaType)) {
                    facadeObjParamBo.setParamJavaTypeCn(EnumDataJavaType.find(paramJavaType)
                        .getDescription());
                }
                if (!StringUtils.isEmpty(isRequired)) {
                    facadeObjParamBo.setIsRequiredCn(EnumYn.find(isRequired)
                        .getDescription());
                }
            }
        }
        return facadeObjParamBos;
    }
}
